package citylife.geotools.demo.tianditu.mkt;

import org.geotools.tile.Tile;
import org.geotools.tile.TileService;
import org.geotools.tile.impl.WebMercatorTileFactory;
import org.geotools.tile.impl.ZoomLevel;
import org.geotools.tile.impl.bing.BingTileUtil;

/**
 * The tile factory implementation for the OpenStreetMap family
 *
 * @author Tobias Sauerwein
 * @author Ugo Taddei
 * @since 12
 */
public class TiandituTileFactory extends WebMercatorTileFactory {
  @Override
  public Tile findTileAtCoordinate(double lon, double lat, ZoomLevel zoomLevel, TileService service) {

    int[] tileXY = BingTileUtil.lonLatToPixelXY(lon, lat, zoomLevel.getZoomLevel());

    int colX = (int) Math.floor(tileXY[0] / TiandituTile.DEFAULT_TILE_SIZE);
    int rowY = (int) Math.floor(tileXY[1] / TiandituTile.DEFAULT_TILE_SIZE);

    return new TiandituTile(colX, rowY, zoomLevel, service);
  }

  @Override
  public Tile findRightNeighbour(Tile tile, TileService service) {
    return new TiandituTile(tile.getTileIdentifier().getRightNeighbour(), service);
  }

  @Override
  public Tile findLowerNeighbour(Tile tile, TileService service) {
    return new TiandituTile(tile.getTileIdentifier().getLowerNeighbour(), service);
  }
}